unit Unit62; 

{$mode objfpc}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Unit1, Unit36, sqldb;

type

  { TForm62 }

  TForm62 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    codigo_broker : string;
    puntos_disponibles : real;
  public
    { public declarations }
  end; 

var
  Form62: TForm62; 

implementation

{ TForm62 }

procedure TForm62.Button1Click(Sender: TObject);
begin
  Form36.ShowModal;

  if Form36.codigo <> '' then
  begin
    codigo_broker := Form36.codigo;
    Edit1.Text := Form36.nombre;

    Label4.Visible := true;

    if SQLQuery1.Active = true then
      SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select score from vw_cliente where cln_cdg = ' + codigo_broker;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      Label4.Caption := Label4.Caption + '  ' + SQLQuery1.Fields[0].AsString + ' ptos.';
      puntos_disponibles := strTofloat(SQLQuery1.Fields[0].AsString);
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    Edit2.Enabled := true;
    Edit3.Enabled := true;

    Edit2.SetFocus;
  end;
end;

procedure TForm62.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm62.Button3Click(Sender: TObject);
var
  QueryString : string;

begin
  if puntos_disponibles < strToInt(Edit3.Text) then
     ShowMessage('Saldo insuficiente!')
  else
  begin
    QueryString := 'insert into retiro_premios(rtp_cliente, rtp_valor, rtp_detalle) values(' + codigo_broker
      + ', ' + Edit3.Text + ', ''' + Edit2.Text + ''')';

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('RETIRO DE PREMIO EXITOSO');

    Close;
  end;
end;

procedure TForm62.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  SQLQuery1.Close;
end;

procedure TForm62.FormShow(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit2.Enabled := false;
  Edit3.Enabled := false;
  Label4.Visible := false;

  Button1.SetFocus;
end;

initialization
  {$I unit62.lrs}

end.

